<?php

class NewsController extends Controller {

    public function actionView($id) {
        try {
            if ($id !== '') {
                $model = News::model()->findByAttributes(array(
                    'slug' => $id
                        ));
                $this->render('view', array(
                    'model' => $model,
                ));
            }
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionIndex() {
        try {
            $criteria = new CDbCriteria();
            $criteria->order = 't.created_date desc';
            $project = News::model()->findAll($criteria);
            $this->render('index', array(
                'project' => $project
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionSearch($q = '') {
        //date_default_timezone_set('UTC');
        
        $start_date = date('Y-m-d', time());
        $str_return = '';
        if ($q != '') {
            $str = MyFunctionCustom::remove_vietnamese_accents($q);
            $str_return = strtolower($str);
        }
        $criteria = new CDbCriteria();
        //$criteria->select = '*';
        //$criteria->compare('t.created_date', '>=' . $start_date);
        $criteria->addCondition('t.slug like "%' . $str_return . '%" OR t.name like "%' . $str_return . '%" OR t.description like "%' . $str_return . '%"');
        //Get all item from gallery of an event
        $search = News::model()->findAll($criteria);
        $this->render('search', array('data' => $search));
    }

    public function loadModel($id) {
        try {
            $model = News::model()->findByPk($id);
            if ($model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
            return $model;
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

}

?>
